<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- tab表格 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :show-index="true"
        index-text="#"
        border
        :selection-type="false"
        :expand-type="false"
        :show-row-hover="false"
        class="treeTab"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted ===false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level==0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level==1">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="CateHandleSizeChange"
        @current-change="CateHandleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1,5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close='addCateDialogClose' >
      <el-form :model="addCateForm" label-width="100px" :rules="rules" ref="addCateFormRef">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- 级联选择器 -->
          <el-cascader
            v-model="selecteKeys"
            :options="ParentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateTrue">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //商品分类列表,默认为空
      cateList: [],
      // 商品总数量，默认为0
      total: 0,
      //表格的各项配置
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok' },
        { label: '排序', type: 'template', template: 'order' },
        { label: '操作', type: 'template', template: 'opt' }
      ],
      // 添加分类对话框的显示隐藏
      addCateDialogVisible: false,
      // 分类对话框数据
      addCateForm: {
        cat_name: '', //将要添加分类的名称
        cat_pid: 0, //父级分类id
        cat_level: 0 //分类层级
      },
      //父级分类的数据
      ParentCateList: [],
      //父级分类选择的数据
      selecteKeys: [],
      //级联选择器的配置
      cascaderProps: {
        checkStrictly: true,
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    //获取商品分类数据
    async getCateList() {
      let { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    //一页数量改变
    CateHandleSizeChange(val) {
      console.log(val)
      this.queryInfo.pagesize = val
      this.getCateList()
    },
    //页码改变
    CateHandleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getCateList()
    },
    // 添加分类
    addCate() {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    //获取父级分类数据
    async getParentCateList() {
      let { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.ParentCateList = res.data
      console.log(this.ParentCateList)
    },
    // 父级分类选项改变
    parentCateChange() {
      if (this.selecteKeys.length > 0) {
        this.addCateForm.cat_pid = this.selecteKeys[this.selecteKeys.length - 1]

        this.addCateForm.cat_level = this.selecteKeys.length
        return
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    // 分类添加对话框确定事件
    addCateTrue() {
      console.log(this.addCateForm)
      this.$refs.addCateFormRef.validate(async boolean=>{
        if(!boolean) return
        let {data:res}= await this.$http.post('categories',this.addCateForm)
        console.log(res)
        if(res.meta.status !==201){
          return this.$message.error(res.meta.msg)
        }
          this.$message.success(res.meta.msg)
          this.getCateList()
          this.addCateDialogVisible=false
      })
    },
    //监听关闭对话框事件
    addCateDialogClose(){
      this.$refs.addCateFormRef.resetFields()
      this.selecteKeys =[]
      this.addCateForm.cat_pid=0
      this.addCateForm.cat_level=0
    }
  }
}
</script>
<style lang="less" scoped>
.treeTab {
  margin-top: 15px;
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
